home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / dosutil / comchk.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  2KB  |  101 lines

  1. /*
  2.  * COMCHK - A program to checksum .COM files
  3.  *
  4.  * Copyright 1990-1994 Dave Dunfield.
  5.  *
  6.  * May be freely distributed and used as long as
  7.  * my copyright notices are retained.
  8.  */
  9. #include <stdio.h>
  10. #include <file.h>
  11.  
  12. #define BUFFER_SIZE    1024
  13.  
  14.     unsigned size, cksum = 0;
  15.     char buffer[BUFFER_SIZE], save_code[3];
  16.     FILE *fpr, *fpw;
  17.  
  18.     char errmsg[] =
  19.         { 'C','o','r','r','u','p','t',' ','i','m','a','g','e',10,13 };
  20.  
  21.     extern char CHK_S[], CHK_E[];    /* Embedded function pointers */
  22.  
  23. /*
  24.  * Write a binary character to a file
  25.  */
  26. write_byte(c)
  27.     char c;
  28. {
  29.     write(&c, 1, fpw);
  30. }
  31.  
  32. /*
  33.  * Main program - Patch the COM file to include the embedded function.
  34.  */
  35. main(argc, argv)
  36.     int argc;
  37.     char *argv[];
  38. {
  39.     unsigned i;
  40.  
  41.     if(argc < 3)
  42.         abort("\nUse: comchk oldfile newfile\n\nCopyright 1990 Dave Dunfield\nFreely Distributable.\n");
  43.  
  44.     if(!(fpr = open(argv[1], F_READ)))
  45.         abort("Cannot open INPUT file");
  46.  
  47.     if(!(fpw = open(argv[2], F_WRITE)))
  48.         abort("Cannot open OUTPUT file");
  49.  
  50. /* Determine the size of the file & save the startup code */
  51.     size = i = read(buffer, BUFFER_SIZE, fpr);
  52.     memcpy(save_code, buffer, 3);
  53.     addchk(buffer, i);
  54.     while(i == BUFFER_SIZE) {
  55.         size += (i = read(buffer, BUFFER_SIZE, fpr));
  56.         addchk(buffer, i); }
  57.  
  58. /* Write out the new startup code that jumps to our embedded function */
  59.     i = (size - 3) + (sizeof(errmsg)+7);
  60.     write_byte(0xE9);
  61.     write_byte(i & 255);
  62.     write_byte(i / 256);
  63.  
  64. /* Write out the remainder of the original file */
  65.     lrewind(fpr);
  66.     i = read(buffer, BUFFER_SIZE, fpr);
  67.     write(&buffer[3], i - 3, fpw);
  68.     while(i == BUFFER_SIZE) {
  69.         i = read(buffer, BUFFER_SIZE, fpr);
  70.         write(buffer, i, fpw); }
  71.  
  72. /* Append various data needed */
  73.     write(save_code, 3, fpw);                /* Original startup code */
  74.     write_byte(cksum & 255);                /* Checksum (LOW) */
  75.     write_byte(cksum / 256);                /* Checksum (HIGH) */
  76.     write_byte(sizeof(errmsg) & 255);        /* Msg size (LOW) */
  77.     write_byte(sizeof(errmsg) / 256);        /* Msg size (HIGH) */
  78.     write(errmsg, sizeof(errmsg), fpw);        /* Error message */
  79.  
  80. /* Append the embedded function */
  81.     size += 0x100;
  82.     write_byte(0xBE);    /* Load SI with address */
  83.     write_byte(size & 255);
  84.     write_byte(size / 256);
  85.     write(CHK_S, CHK_E - CHK_S, fpw);
  86.  
  87.     close(fpr);
  88.     close(fpw);
  89. }
  90.  
  91. /*
  92.  * Add contents of buffer to checksum
  93.  */
  94. addchk(ptr, size)
  95.     unsigned char *ptr;
  96.     unsigned size;
  97. {
  98.     while(size--)
  99.         cksum += *ptr++;
  100. }
  101.